package psn.zjk.common;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Semaphore;

/**
 * @author zhangjk
 * @CREATE_TIME 2021/1/18 19:44
 */
public class Main {

    static class PrintTask implements Runnable{
        String word;
        Semaphore next;
        Semaphore self;

        public PrintTask(String word, Semaphore self,Semaphore next) {
            this.word = word;
            this.next = next;
            this.self = self;
        }

        @Override
        public void run() {
            int i=10;
            while(i-->0){
                try {
                    self.acquire();
                    System.out.println(word);
                    next.release();


                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
        Semaphore semaphoreA = new Semaphore(1);
        Semaphore semaphoreB = new Semaphore(0);
        Semaphore semaphoreC = new Semaphore(0);



        new Thread(new PrintTask("A",semaphoreA,semaphoreB)).start();
        new Thread(new PrintTask("B",semaphoreB,semaphoreC)).start();
        new Thread(new PrintTask("C",semaphoreC,semaphoreA)).start();

        while(true){

        }

    }
}